#include "WiFiManager.h"
#include "../../application/App.h"

bool WiFiManager::connect() {
    // 设置为Station模式
    WiFi.mode(WIFI_STA);
    // 开始连接
    WiFi.begin(SSID, PASSWORD);
    while ((_status = WiFi.status()) != WL_CONNECTED) {
        // 每隔500ms检查一次
        delay(500);
    }
    return _status == WL_CONNECTED;
}

void WiFiManager::disconnect() {
    WiFi.disconnect();
    _status = WL_DISCONNECTED;  // 更新状态
}

bool WiFiManager::isConnected() {
    _status = WiFi.status();  // 主动获取最新状态
    return _status == WL_CONNECTED;
}

const char* WiFiManager::getStatusString() {
    switch (_status) {
        case WL_NO_SHIELD:
            return "WiFi not enable";
        case WL_IDLE_STATUS:
            return "Idle state";
        case WL_NO_SSID_AVAIL:
            return "Not find this WIFI";
        case WL_CONNECTED:
            return "Connected";
        case WL_CONNECT_FAILED:
            return "connection failed";
        case WL_CONNECTION_LOST:
            return "connection lost";
        case WL_DISCONNECTED:
            return "not connected";
        default:
            return "unknown";
    }
}

String WiFiManager::getLocalIP() {
    return WiFi.localIP().toString();  // 返回IP字符串
}